<?php
namespace Admin\Survey\Controllers;

use Admin\Models\overt\XhovertValidcode;
use Admin\Models\stats\XhstatsWelfare;
use Helpers\Main;
use Helpers\Tool;
use Helpers\Assets;

class WelfareController extends ControllerBase
{
    private $typeEnum = [
        'kokugaku' => 1,
        'summerSafe' => 2
    ];
    private $kokugakuExtra = [
        'grade' => 'sExtraVal1',
        'term'  => 'sExtraVal2'
    ];

    /**
     * 寒假公益课程计划-学海国学讲堂介绍页面
     *
     * create by ck 20180207
     */
    public function kokugakuAction()
    {
        $this->view->pick('welfare/kokugaku');
        Assets::importMainJs('assets/xh/xhTop.js');
        Assets::importMainJs('assets/xh/xhTool.js');
        Assets::importMainJs('assets/xh/xhExtra.js');
        Assets::sweetAlert2('js/welfare/kokugaku.js');
    }

    /**
     * 寒假公益课程计划-学海国学讲堂报名页面
     *
     * create by ck 20180207
     */
    public function kokugakuSignAction()
    {
        $this->view->pick('welfare/kokugakuSign');
        Assets::importMainJs('assets/xh/xhTop.js');
        Assets::importMainJs('assets/xh/xhTool.js');
        Assets::importMainJs('assets/xh/xhExtra.js');
        Assets::sweetAlert2('js/welfare/kokugakuSign.js');
    }

    /**
     * 寒假公益课程计划-学海国学讲堂保存
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20180208
     */
    public function saveKokugakuAction()
    {
        $startTime = strtotime('2018-02-09 00:00:00');
        $endTime   = strtotime('2018-02-12 00:00:00');
        $nowTime   = time();
        if ($nowTime < $startTime || $nowTime > $endTime) {
            return $this->sendErrorResponse(400, '未到报名时间或报名已结束');
        }
        $userId     = $this->request->getPost('userId', 'int');
        $giveUp     = !!$this->request->getPost('giveUp', 'int');
        $phoneNum   = $this->request->getPost('phoneNum', 'int');
        $validCode  = $this->request->getPost('validCode', 'int');
        $grade      = $this->request->getPost('grade', 'int');
        $term       = $this->request->getPost('term', 'int');
        if (!$userId) {
            return $this->sendErrorResponse(400, '缺少用户信息');
        }
        try {
            if (XhstatsWelfare::checkExist($userId, $this->typeEnum['kokugaku'])) {
                return $this->sendErrorResponse(400, '您已提交过，请勿重复提交');
            }
            $obj = new XhstatsWelfare();
            $obj->iUserId = $userId;
            $obj->iType   = $this->typeEnum['kokugaku'];
            if ($giveUp) {
                $obj->bQuit   = 1;
            }
            else {
                if (!$validCode) {
                    return $this->sendErrorResponse(400, '缺少验证码');
                }
                if (!$grade) {
                    return $this->sendErrorResponse(400, '缺少年级信息');
                }
                if (!$term) {
                    return $this->sendErrorResponse(400, '缺少期次');
                }
                if (!Main::checkValidCode($phoneNum, $validCode)) {
                    return $this->sendErrorResponse(400, '验证码错误或已失效');
                }
                $obj->bQuit                             = 0;
                $obj->sPhoneNum                         = $phoneNum;
                $obj->{$this->kokugakuExtra['grade']}   = $grade;
                $obj->{$this->kokugakuExtra['term']}    = $term;
            }
            if (!$obj->save()) {
                return $this->sendErrorResponse(500, '保存失败', $obj->getMessages()[0]);
            }
            return $this->sendSuccessResponse('提交成功，感谢参加');
        }
        catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }

    /**
     * 感谢页面
     *
     * create by ck 20180208
     */
    public function thankAction()
    {
        $this->view->pick('welfare/thank');
    }

    /**
     * 获取验证码
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20180208
     */
    public function getValidCodeAction()
    {
        $phoneNum = $this->request->getPost('phoneNum', 'string');
        if (!$phoneNum) {
            return $this->sendErrorResponse(400, '手机号不能为空');
        }
        try {
            Main::sendValidCode($phoneNum);
            return $this->sendSuccessResponse('验证码发送成功，请查收');
        }
        catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }

    /**
     * 记住暑期安全提醒
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20180626
     */
    public function saveSummerSafeAction()
    {
        $userId = $this->request->getPost('userId', 'int');
        if (!$userId) {
            return $this->sendErrorResponse(200, '缺少用户id');
        }
        try {
            $safe = new XhstatsWelfare();
            $safe->iUserId = $userId;
            $safe->iType   = $this->typeEnum['summerSafe'];
            if (!$safe->save()) {
                throw new \Exception($safe->getError());
            }
            return $this->sendSuccessResponse('已记住提醒');
        }
        catch (\Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }
}

